package com.hpin.mlxteacher.fragment;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.NewsItemAdapter;
import com.hpin.mlxteacher.base.BaseFragment;
import com.hpin.mlxteacher.message.EditActivity;
import com.hpin.mlxteacher.message.SearchActivity;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.DensityUtil;
import com.hpin.mlxteacher.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息fragment
 * Created by Aaron on 2015/7/15.
 */
public class NewsFragment extends BaseFragment implements View.OnClickListener {
    private ListView lv;
    private ImageView iv_news_choose;
    private ImageView iv_edit;
    private ImageView iv_search;

    //随着标题滑动的红线
    private TextView tv_red_line;
    private int screenwidth;
    /**当前显示的 是消息还是待办还是通知 的 角标*/
    private int currentMessageIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View newsView = inflater.inflate(R.layout.fragment_news, null);

        LogUtil.d("TAG", "==========onCreateView=========");

        currentMessageIndex = 0;
        initWidget(newsView);

        return newsView;
    }

    private void initWidget(View newsView) {

        lv = (ListView) newsView.findViewById(R.id.lv);

        iv_news_choose = (ImageView) newsView.findViewById(R.id.iv_news_choose);
        iv_edit = (ImageView) newsView.findViewById(R.id.iv_edit);
        iv_search = (ImageView) newsView.findViewById(R.id.iv_search);


        TextView tv_news = (TextView) newsView.findViewById(R.id.tv_choose_left);
        TextView tv_todo = (TextView) newsView.findViewById(R.id.tv_choose_middle);
        TextView tv_notice = (TextView) newsView.findViewById(R.id.tv_choose_right);

        tv_news.setText(getResources().getString(R.string.rl_news));
        tv_todo.setText(getResources().getString(R.string.message_todo));
        tv_notice.setText(getResources().getString(R.string.message_notice));

        tv_red_line = (TextView) newsView.findViewById(R.id.tv_red_line);

        screenwidth = CommonUtils.getScreenSize(getActivity())[0];

        tv_news.setOnClickListener(this);
        tv_todo.setOnClickListener(this);
        tv_notice.setOnClickListener(this);

        iv_news_choose.setOnClickListener(this);
        iv_edit.setOnClickListener(this);
        iv_search.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.iv_news_choose:
            //选择收件箱还是发件箱

              chooseMessageStyle(v);

            break;
            case R.id.iv_edit:
              //编辑信息

                Intent edIntent = new Intent(getActivity(), EditActivity.class);
                getActivity().startActivity(edIntent);

            break;


            case R.id.iv_search:
                //搜索
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                getActivity().startActivity(intent);


            break;

            case R.id.tv_choose_left:
                //消息列表
                changeRedLine(currentMessageIndex, 0);

            break;

            case R.id.tv_choose_middle:
            //待办事项
                changeRedLine(currentMessageIndex, 1);
                List<String> size = new ArrayList<String>();
                size.add("124");
                size.add("124");
                size.add("124");
                size.add("124");
                size.add("124");
                size.add("124");
                size.add("124");

                NewsItemAdapter newsItemAdapter = new NewsItemAdapter(getActivity(), size);
                lv.setAdapter(newsItemAdapter);

            break;
            case R.id.tv_choose_right:
                   //通知
                changeRedLine(currentMessageIndex, 2);
                List<String> sizes = new ArrayList<String>();
                sizes.add("124");
                sizes.add("124");

                NewsItemAdapter newItemAdapter = new NewsItemAdapter(getActivity(), sizes);
                lv.setAdapter(newItemAdapter);

            break;

        }

    }

    /**
     * 选择是收件箱还是发件箱
     */
    private void chooseMessageStyle(View v) {

        View popuView = View.inflate(getActivity(), R.layout.popup_choose_news, null);

        final PopupWindow areaPup = new PopupWindow(popuView,
                DensityUtil.dip2px(getActivity(), 120), LayoutParams.WRAP_CONTENT);
//        areaPup.setContentView(popuView);
        areaPup.setOutsideTouchable(true);

        areaPup.setFocusable(true);
        areaPup.setBackgroundDrawable(new BitmapDrawable());
        //x y
        areaPup.showAsDropDown(v, 0, DensityUtil.dip2px(getActivity(), 12));

       RelativeLayout rl_receiver = (RelativeLayout) popuView.findViewById(R.id.rl_receiver);
       RelativeLayout rl_send = (RelativeLayout) popuView.findViewById(R.id.rl_send);

        rl_receiver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (areaPup!=null && areaPup.isShowing())
                    areaPup.dismiss();

            }
        });

        rl_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (areaPup!=null && areaPup.isShowing())
                    areaPup.dismiss();

            }
        });

    }


    private void changeRedLine(int currentIndex, int toIndex) {

        ObjectAnimator anim = null;
        //获取当前控件的x距离
        float  curTranslationX = tv_red_line.getTranslationX();

//        LogUtil.e("TAG", "curTranslationX==>" + curTranslationX);

        switch (currentIndex) {

            case 0:

                //当前在第一位置
                switch (toIndex) {

                    case 1 :
                        //1, 需要动画的控件. 2,动画名称.  3 控件当前的位置 4, 移动到哪个位置
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth/3);
                        currentMessageIndex = 1;
                        break;

                    case 2:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth/3 * 2);
                        currentMessageIndex=2;
                        break;

                }

                break;

            case 1:

                //当前在第二位置
                switch (toIndex) {

                    case 0 :

                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, 0);
                        currentMessageIndex=0;
                        break;

                    case 2:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth/3*2);
                        currentMessageIndex=2;
                        break;

                }

              break;

            case 2:

                //当前在第三位置
                switch (toIndex) {

                    case 0 :
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, 0);
                        currentMessageIndex=0;
                        break;

                    case 1:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth/3);
                        currentMessageIndex=1;
                        break;

                }

                break;

        }

        if (anim!=null) {
            //设置移动时间0.5秒s
            anim.setDuration(500);
            anim.start();
        }

    }


}
